package com.idp.web.system.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.common.base.BaseController;
import com.common.constant.SessionAttr;
import com.common.enums.CommonCode;
import com.idp.biz.bppost.entity.BpPost;
import com.idp.biz.bppost.service.BpPostService;
import com.idp.biz.bpuser.entity.BpUser;
import com.idp.biz.bpuser.service.BpUserService;

/**
 * 首页controller
 * 
 * @author Stone
 *
 */
@Controller
@RequestMapping("/")
public class IndexController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(IndexController.class);
	
	@Autowired
    private BpUserService userService;
	@Autowired
	private BpPostService postService;

	/**
	 * 主页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String login(HttpServletRequest request) {
		String userMenu = (String) request.getSession().getAttribute(SessionAttr.USER_MENUS.getValue());
		request.setAttribute("menus", userMenu);
		logger.info("进入主页面");
		return "main";
	}

	/**
	 * 首页
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "home", method = RequestMethod.GET)
	public String home(HttpServletRequest request) {
		//用户总数
		BpUser bpUser = new BpUser();
		bpUser.setSymbol(CommonCode.Y.getValue());
		List<BpUser> listUser = userService.findBySearch(bpUser);
		//帖子总数
		BpPost bpPost = new BpPost();
		bpPost.setSymbol(CommonCode.Y.getValue());
		List<BpPost> listPost = postService.findBySearch(bpPost);
		
		request.setAttribute("userCount", listUser.size());
		request.setAttribute("postCount", listPost.size());
		return "pages/home";
	}

}
